#define USE_DotNet2

using System;
using System.Collections.Generic;
using System.Text;

namespace SharpMud.Terminal
{
	public class ConsoleWrapper : IOutputTerminal, IInteractiveTerminal
	{
        private const string Err_NotImplemented = "The method or operation is not implemented for the .NET runtime that it was compiled for.";

        #region Constructors
        public ConsoleWrapper()
        {
        }
        #endregion

        #region IOutputTerminal Members

        void IOutputTerminal.Beep()
        {
#if USE_DotNet2
            Console.Beep();
#else
            Console.Write(System.Convert.ToChar((byte)7);
#endif
        }

        void IOutputTerminal.Clear()
        {
#if USE_DotNet2
            Console.Clear();
#else
            for(int x=0;x<100;x++)
                Console.WriteLine();
#endif
        }

        void IOutputTerminal.ResetColor()
        {
#if USE_DotNet2
            Console.ResetColor();
#else
            throw new Exception(Err_NotImplemented);
#endif
        }

        void IOutputTerminal.Write(string value)
        {
            Console.Write(value);
        }

        void IOutputTerminal.Write(char value)
        {
            Console.Write(value);
        }

        void IOutputTerminal.Write(char[] buffer)
        {
            Console.Write(buffer);
        }

        void IOutputTerminal.Write(char[] buffer, int start, int length)
        {
            Console.Write(buffer, start, length);
        }

        void IOutputTerminal.WriteLine(char value)
        {
            Console.WriteLine(value);
        }

        void IOutputTerminal.WriteLine(string value)
        {
            Console.WriteLine(value);
        }

        void IOutputTerminal.WriteLine(char[] buffer)
        {
            Console.WriteLine(buffer);
        }

        void IOutputTerminal.WriteLine(char[] buffer, int start, int length)
        {
            Console.WriteLine(buffer, start, length);
        }

        void IOutputTerminal.WriteLine()
        {
            Console.WriteLine();
        }

        TerminalColor IOutputTerminal.BackgroundColor
        {
            get
            {

#if USE_DotNet2
                return ColorUtility.ToTerminalColor(Console.BackgroundColor);
#else
            throw new Exception(Err_NotImplemented);
#endif
            }
            set
            {
#if USE_DotNet2
                Console.BackgroundColor = ColorUtility.ToConsoleColor(value);
#else
            throw new Exception(Err_NotImplemented);
#endif
            }
        }

        TerminalColor IOutputTerminal.ForegroundColor
        {
            get
            {

#if USE_DotNet2
                return ColorUtility.ToTerminalColor(Console.ForegroundColor);
#else
                throw new Exception(Err_NotImplemented);
#endif
            }
            set
            {
#if USE_DotNet2
                Console.ForegroundColor = ColorUtility.ToConsoleColor(value);
#else
                throw new Exception(Err_NotImplemented);
#endif
            }
        }

        Encoding IOutputTerminal.OutputEncoding
        {
            get
            {
#if USE_DotNet2
                return Console.OutputEncoding;
#else
                throw new Exception(Err_NotImplemented);
#endif
            }
            set
            {
#if USE_DotNet2
                Console.OutputEncoding = value;
#else
                throw new Exception(Err_NotImplemented);
#endif
            }
        }

        #endregion

        #region IInteractiveTerminal Members

        char IInteractiveTerminal.Read()
        {
            return (char)Console.In.Read();
        }

        int IInteractiveTerminal.Peek()
        {
            return Console.In.Peek();
        }

        string IInteractiveTerminal.ReadLine()
        {
            return Console.ReadLine();
        }

        Encoding IInteractiveTerminal.InputEncoding
        {
            get
            {
#if !USE_DotNet2
                throw new Exception(Err_NotImplemented);
#else
                return Console.InputEncoding;
#endif
            }
            set
            {
#if !USE_DotNet2
                throw new Exception(Err_NotImplemented);
#else
                Console.InputEncoding = value;
#endif
            }
        }

        #endregion
    }
}
